<?php

namespace Crc\Tools;

use Doctrine\Common\Annotations\AnnotationReader;

class Doctrine2
{
    /**
    * Annotation reader.
    * @var Doctrine\Common\Annotations\AnnotationReader
    */
    protected static $_annotationReader__;

    /**
    * Gets annotations of a given namespace for a property.
    *
    * @param ReflectionProperty $property
    * @param string $annotationName
    * @return array
    */
    protected static function _getPropertyAnnotation($property, $annotationName = NULL)
    {
        $reader = self::_getAnnotationReader();
        $annotations = $reader->getPropertyAnnotations($property);

        if ($annotationName !== NULL) {
            $result = array();
            foreach ($annotations as $annotation) {
                if ($annotation instanceof $annotationName) {
                    $result[] = $annotation;
                }
            }

            return $result;
        }
        return $annotations;
    }

    /**
    * Gets the Doctrine annotation reader.
    *
    * @return Doctrine\Common\Annotations\AnnotationReader
    */
    protected static function _getAnnotationReader()
    {
        if (null === self::$_annotationReader__) {
            self::$_annotationReader__ = new AnnotationReader;
        }
        return self::$_annotationReader__;
    }
}